package com.zut.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zut.dao.MemberDao;
import com.zut.pojo.Member;
import com.zut.service.MemberService;
import com.zut.service.OrderService;
import com.zut.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员服务
 * @author sq
 * @create 2021-07-15-17:03
 */
//添加事务注解之后，必须明确当前这个服务实现的是哪一个服务接口
@Service(interfaceClass = MemberService.class)
//事务注解
@Transactional
//实现health_interface中的接口
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询用户
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        return member;
    }

    //添加会员
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用工具类的md5方法将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员的数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            //将日期拼接成数据库日期格式
            String date = month + ".31";
            //获取小于等于当前日期的会员数量
            Integer memberCount = memberDao.findMemberCountBeforeDate(date);
            //添加到list集合中
            list.add(memberCount);
        }
        return list;
    }
}
